package com.zsd.android.common

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible

abstract class BaseActivity<V : IBaseView, M : BaseModel<*>, P : BasePresenter<V, M>> :
    AppCompatActivity() {

    protected val mContext: Activity = this
    protected var mPresenter: P? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId())
        mPresenter = initPresenter()
        initView(savedInstanceState)
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter?.destroy()
    }

    /**
     * 显示toast
     *
     * @param msg 显示内容
     */
    protected fun showToast(msg: String?) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
    }

    /**
     * 显示View
     *
     * @param view 要显示的View
     */
    protected fun showView(view: View) {
        if (!view.isVisible) {
            view.visibility = View.VISIBLE
        }
    }

    /**
     * 隐藏view
     *
     * @param view 要隐藏的View
     * @param visibility 隐藏方式，默认是GONE，也可以是INVISIBILITY
     */
    protected fun hideView(view: View, visibility: Int = View.GONE) {
        if (view.isVisible) {
            view.visibility = visibility
        }
    }

    abstract fun layoutId(): Int

    abstract fun initPresenter(): P?

    abstract fun initView(savedInstanceState: Bundle?)
}